package o;

import android.util.Log;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

public abstract class dng {
    private static dne ˊ = dne.INFO;
    private static String ˋ = System.getProperty("line.separator");
    static int ˎ = 0;
    private static dne ˏ = dne.ERROR;
    private long ʻ;
    private String ʼ;
    private boolean ʽ = false;
    private final Map<String, dne> ॱ = new HashMap();
    private SimpleDateFormat ॱॱ = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.ENGLISH);
    private int ᐝ = 15;

    public abstract boolean ˋ(String str, dne o_dne);

    public abstract void ॱ(dnd o_dnd);

    public dng(String str, long j) {
        this.ʼ = str;
        this.ʻ = j;
    }

    private static dne ˎ() {
        return ˊ;
    }

    private static void ˋ(dne o_dne) {
        ˊ = o_dne;
    }

    public void ˏ(dne o_dne) {
        synchronized (this) {
            ˋ(o_dne);
            for (Entry value : this.ॱ.entrySet()) {
                value.setValue(o_dne);
            }
        }
        dnd.ˋ().ॱ((Object) "all = ").ॱ((Object) o_dne).ʽ();
    }

    public synchronized dne ॱ(String str) {
        dne o_dne;
        o_dne = (dne) this.ॱ.get(str);
        if (o_dne == null) {
            o_dne = ˎ();
        }
        return o_dne;
    }

    public synchronized void ˏ(String str) {
        this.ʼ = str;
    }

    public long ˏ() {
        return this.ʻ;
    }

    public int ˋ() {
        return this.ᐝ;
    }

    protected synchronized void ˊ(String str) {
        boolean z = true;
        boolean z2 = false;
        synchronized (this) {
            if (this.ʼ != null) {
                String str2 = null;
                long ˏ = ˏ();
                File file = new File(this.ʼ);
                if (!file.exists()) {
                    file.setReadable(true);
                    file.setWritable(true);
                    file.setExecutable(false, false);
                }
                if (ˏ > 0) {
                    File parentFile = file.getParentFile();
                    if (parentFile == null || parentFile.exists() || parentFile.mkdirs()) {
                        if (file.length() + ((long) str.length()) > ˏ) {
                            parentFile = new File(this.ʼ + ".bak");
                            if (parentFile.exists()) {
                                ˊ(parentFile, this.ʼ + "." + this.ॱॱ.format(Long.valueOf(System.currentTimeMillis() - 3000)));
                            }
                            str2 = this.ʼ + "." + this.ॱॱ.format(Long.valueOf(System.currentTimeMillis()));
                            z2 = ˊ(file, str2);
                            ˊ(file, str, z, z2, str2 + ".zip");
                            ˎ++;
                        }
                    }
                }
                z = false;
                ˊ(file, str, z, z2, str2 + ".zip");
                ˎ++;
            }
        }
    }

    private boolean ˊ(File file, String str) {
        File file2;
        File[] listFiles = file.getParentFile().listFiles();
        if (listFiles != null && listFiles.length >= ˋ() + 1) {
            Object arrayList = new ArrayList();
            HashMap hashMap = new HashMap();
            for (File file3 : listFiles) {
                if (file3 != null && file3.exists() && file3.getName().endsWith("zip")) {
                    long lastModified = file3.lastModified();
                    arrayList.add(Long.valueOf(lastModified));
                    hashMap.put(Long.valueOf(lastModified), file3);
                }
            }
            if (arrayList.size() >= ˋ()) {
                Collections.sort(arrayList);
                file2 = (File) hashMap.get(arrayList.get(0));
                if (!file2.delete()) {
                    dnp.ॱ("LoggerBase", "zip delete failed!:" + file2.getName());
                }
            }
        }
        file2 = new File(str);
        boolean renameTo = file.renameTo(file2);
        dnw.ˊ(str, str + ".zip");
        dnw.ˊ(file2);
        return renameTo;
    }

    private void ˊ(File file, String str, boolean z, boolean z2, String str2) {
        Closeable fileOutputStream;
        Closeable outputStreamWriter;
        Throwable th;
        Throwable th2;
        Closeable closeable = null;
        try {
            fileOutputStream = new FileOutputStream(file, true);
            try {
                outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
                if (z) {
                    Object obj;
                    if (z2) {
                        try {
                            obj = "success";
                        } catch (FileNotFoundException e) {
                            closeable = outputStreamWriter;
                            try {
                                Log.e("LoggerBase", "println error, error:FileNotFoundException");
                                dnw.ˎ(closeable);
                                dnw.ˎ(fileOutputStream);
                            } catch (Throwable th3) {
                                th = th3;
                                outputStreamWriter = closeable;
                                closeable = fileOutputStream;
                                th2 = th;
                                dnw.ˎ(outputStreamWriter);
                                dnw.ˎ(closeable);
                                throw th2;
                            }
                        } catch (IOException e2) {
                            try {
                                Log.e("LoggerBase", "println error, error:IOException");
                                dnw.ˎ(outputStreamWriter);
                                dnw.ˎ(fileOutputStream);
                            } catch (Throwable th4) {
                                th = th4;
                                closeable = fileOutputStream;
                                th2 = th;
                                dnw.ˎ(outputStreamWriter);
                                dnw.ˎ(closeable);
                                throw th2;
                            }
                        }
                    }
                    obj = "failure";
                    Object ˊ = dmx.ˊ();
                    ˊ.ˏ("zip to ").ˏ(str2).ˏ(HwAccountConstants.BLANK).ˏ(obj);
                    if (ˎ > 0) {
                        ˊ.ˋ().ˏ(dnd.ॱ(false));
                    }
                    outputStreamWriter.write(dnd.ˋ().ॱ(ˊ).toString());
                    outputStreamWriter.write(ˋ);
                }
                outputStreamWriter.write(str);
                outputStreamWriter.write(ˋ);
                outputStreamWriter.flush();
                dnw.ˎ(outputStreamWriter);
                dnw.ˎ(fileOutputStream);
            } catch (FileNotFoundException e3) {
                Log.e("LoggerBase", "println error, error:FileNotFoundException");
                dnw.ˎ(closeable);
                dnw.ˎ(fileOutputStream);
            } catch (IOException e4) {
                outputStreamWriter = null;
                Log.e("LoggerBase", "println error, error:IOException");
                dnw.ˎ(outputStreamWriter);
                dnw.ˎ(fileOutputStream);
            } catch (Throwable th32) {
                th = th32;
                outputStreamWriter = null;
                closeable = fileOutputStream;
                th2 = th;
                dnw.ˎ(outputStreamWriter);
                dnw.ˎ(closeable);
                throw th2;
            }
        } catch (FileNotFoundException e5) {
            fileOutputStream = null;
            Log.e("LoggerBase", "println error, error:FileNotFoundException");
            dnw.ˎ(closeable);
            dnw.ˎ(fileOutputStream);
        } catch (IOException e6) {
            fileOutputStream = null;
            outputStreamWriter = null;
            Log.e("LoggerBase", "println error, error:IOException");
            dnw.ˎ(outputStreamWriter);
            dnw.ˎ(fileOutputStream);
        } catch (Throwable th5) {
            th2 = th5;
            outputStreamWriter = null;
            dnw.ˎ(outputStreamWriter);
            dnw.ˎ(closeable);
            throw th2;
        }
    }

    public String ˊ() {
        return null;
    }

    protected void ˏ(String str, dne o_dne, String str2) {
        if (ˎ(str, o_dne)) {
            ˊ(str2);
        }
    }

    public boolean ˎ(String str, dne o_dne) {
        return o_dne.ॱ() >= ॱ(str).ॱ();
    }

    public boolean ॱ() {
        return this.ʽ;
    }

    public void ˊ(boolean z) {
        this.ʽ = z;
    }
}
